package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.model.Card;
import com.qf.model.Massage;
import com.qf.model.User;
import com.qf.model.pojo.UserCard;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet("/userServlet")
public class UserServlet extends BascServlet {
    UserService userService = new UserServiceImpl();

    //查询身份证是否已注册
    protected void findUserByU_idcard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取手机号
        String u_idcard = req.getParameter("u_idcard");
        //调用查询身份证是否已注册的方法
        User user = userService.findUserByU_idcard(u_idcard);

        Massage m = null;
        if (user == null) {
            m = new Massage(-1, "可以注册");
        } else {
            m = new Massage(1, "该身份证号已有账户，是否跳转到登录界面?注册账号手机号为：" + user.getU_phone());
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //查询手机号是否已注册
    protected void findUserByU_phone(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取手机号
        String phone = req.getParameter("u_phone");
        //调用查询身份证是否已注册的方法
        User user = userService.findUserByphone(phone);

        Massage m = null;
        if (user == null) {
            m = new Massage(-1, "可以注册");
        } else {
            m = new Massage(1, "该手机号已被注册，请更换手机号或去登录");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //添加注册的新的用户信息
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String code = req.getParameter("code");
        String u_phone = req.getParameter("u_phone");
        String u_pwd = req.getParameter("password");
        String u_idcard = req.getParameter("u_idcard");
        String u_name = req.getParameter("u_name");
        //判断验证码,从session域中取到验证码
        String attribute = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        Massage<UserCard> m = null;
        if (attribute.equals(code)) {
            //检查是否已经注册账号
            User user = userService.findUserByU_idcard(u_idcard);
            User user1 = userService.findUserByphone(u_phone);
            if (user != null) {
                m = new Massage<>(-1, "已有账号,不能重复注册！");
            } else {
                if (user1 != null) {
                    m = new Massage<>(-1, "手机号已被注册，请更换手机号");
                } else {
                    //添加注册用户
                    int update = userService.addUser(u_phone, u_pwd, u_idcard, u_name);
                    if (update > 0) {
                        m = new Massage<>(1, "注册成功,是否去登陆");
                    } else {
                        m = new Massage<>(-1, "注册失败，请重新注册");
                    }
                }
            }
        } else {
            m = new Massage<>(-2, "验证码错误");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //根据u_id查询用户信息
    protected void findUserByU_id(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户id
        int u_id = Integer.parseInt(req.getParameter("userid"));
        //调用查询方法
        User user = userService.findByUid(u_id);
        Massage<User> m = null;
        if (user != null) {
            m = new Massage<>(1, "查询成功", user);
        } else {
            m = new Massage<>(-1, "查询失败");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);

    }


    //根据用户id查询用户银行卡信息
    public void findCardByUid(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        //获取用户id
        int u_id = Integer.parseInt(req.getParameter("userid"));
        //调用查询方法
        List<Card> list = userService.findCardByUid(u_id);
        Massage<List<Card>> m = null;
        m = new Massage<>(1, "查询成功", list);
//
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //个人信息  修改
    public void  updateUserByUid(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        int uid=Integer.parseInt(req.getParameter("uid"));
        String u_sex=req.getParameter("u_sex");
        String u_email=req.getParameter("u_email");
        String u_address=req.getParameter("u_address");
        String pwd=req.getParameter("pwd");

        int i = userService.updateUserByUid(uid, u_sex, u_email, u_address, pwd);
        Massage m=null;
        if(i==1){
            m=new Massage(1,"修改成功");
        }else {
            m=new Massage(-1,"修改失败");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //登录
    public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String phone = req.getParameter("phone");
        String pwd = req.getParameter("pwd");
        String code = req.getParameter("code");
        Massage<User> m=null;
        String attribute = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        if(attribute.equals(code)){
            User user= userService.findUserByphone(phone);

            if(user==null){
                m=new Massage<>(-1,"改手机号没有被注册，请先注册");
            }else {
                if(user.getU_pwd().equals(pwd)){
                    req.getSession().setAttribute("user",user);
                    m=new Massage<>(1,"登录成功",user);
                }else {
                    m=new Massage<>(-1,"账号密码有误");
                }
            }

        }else {
            m=new Massage<>(-1,"验证码有误");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);

    }

    //查看登录着信息
    public  void finduser(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        User user =(User) req.getSession().getAttribute("user");
        Massage<User> m=null;
        if(user==null){
            m=new Massage<>(-1,"未登录");
        }else {
            m=new Massage<>(1,"登录成功",user);
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }
}
